/**
 *
 * Copyright by Samuel Castro & Joaquim Fonseca, 2011
 * All Rights Reserved
 *
 * NOTES:
 * Please do not redistribute this plugin without my permission
 * Plugin made for private use only
 *
 */
package com.SySammy.SdCurrency.commands;

import com.SySammy.SdCurrency.Handlers.CurrencyHandler;
import com.SySammy.SdCurrency.Prop.CurrencyProperties;
import com.SySammy.SdCurrency.sdC;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class sdCMoneyCmds implements CommandExecutor {

    private final sdC plugin;
    private CurrencyHandler CH = new CurrencyHandler();

    public sdCMoneyCmds(sdC plugin) {
        this.plugin = plugin;
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] string) {
        String[] args = string;
        String cmd = command.getName().toLowerCase();
        if (cmd.equals("money")) {
            return moneyMethod(sender, args);
        }
        return true;
    }

    private boolean moneyMethod(CommandSender sender, String[] string) {
        String[] args = string;
        Player ply = (Player) sender;
        if (args.length <= 0) {
            if (!plugin.casUseMoneyInfo(ply)) {
                ply.sendMessage(ChatColor.RED + "You don't have permisson to use that command");
                return true;
            }
            sender.sendMessage(ChatColor.DARK_AQUA + "Your pocket contains: " + ChatColor.GOLD + CH.GetMoney(ply.getName()) + " " + CurrencyProperties.Currency);
            return true;
        } else if (ply.isOp() && args[0].equalsIgnoreCase("-c") && args.length >= 2) {
            if (!plugin.casUseMoneyCheck(ply)) {
                ply.sendMessage(ChatColor.RED + "You don't have permisson to use that command");
                return true;
            }
            try {
                String receiverName = args[1].toLowerCase();
                if (CH.hasAccount(receiverName) == true) {
                    sender.sendMessage(ChatColor.DARK_AQUA + receiverName + "'s pocket contains: " + ChatColor.GOLD + CH.GetMoney(receiverName) + " " + CurrencyProperties.Currency);
                } else {
                    sender.sendMessage((ChatColor.RED + "That player doesn't have an account."));
                }
                return true;
            } catch (Exception e) {
                sender.sendMessage(ChatColor.RED + "Player not available at this moment.");
            }

        } else if (args[0].equalsIgnoreCase("-p") && args.length >= 3) {
            if (!plugin.casUseMoneyPay(ply)) {
                ply.sendMessage(ChatColor.RED + "You don't have permisson to use that command");
                return true;
            }
            String receiverName = args[1].toLowerCase();
            if (CH.hasAccount(receiverName) == false) {
                sender.sendMessage((ChatColor.RED + "That player doesn't have an account."));
                return true;
            }
            if (!CH.HasMoney(ply.getName(), Integer.parseInt(args[2]))) {
                sender.sendMessage(ChatColor.RED + "You don't have that much money.");
                return true;
            }
            try {
                int amount = Integer.parseInt(args[2]);
                CH.GiveMoney(receiverName, amount);
                CH.TakeMoney(ply.getName(), amount);
                sender.sendMessage(ChatColor.GOLD + "You sent " + amount + " " + CH.GetCurrency() + " to " + receiverName);
                Player receiver = (Player) sender.getServer().getPlayer(args[1]);
                receiver.sendMessage(ChatColor.GOLD + "You received " + amount + " " + CH.GetCurrency() + " from " + ply.getName());
                return true;
            } catch (Exception e) {
            }
        } else if (args[0].equalsIgnoreCase("-h")) {
            if (!plugin.casUseMoneyHelp(ply)) {
                ply.sendMessage(ChatColor.RED + "You don't have permisson to use that command");
                return true;
            }
            return false;
        } else if (args[0].equalsIgnoreCase("-cSet") && ply.isOp() && args.length >= 2) {
            CurrencyProperties.loadCurrencyProps(true, Integer.parseInt(args[1]));
            ply.sendMessage(ChatColor.GREEN + "Server currency value changed to: " + ChatColor.GOLD + Integer.parseInt(args[1]));
        } else if (args[0].equalsIgnoreCase("-g") && ply.isOp() && args.length >= 3) {
            Player luckyguy = plugin.getServer().getPlayer(args[1]);
            if (luckyguy != null) {
                CH.GiveMoney(args[1], Integer.parseInt(args[2]));
                ply.sendMessage(ChatColor.GREEN + "You just send " + ChatColor.GOLD + Integer.parseInt(args[2]) + " " + CurrencyProperties.Currency + ChatColor.GREEN + " to " + args[1]);
                luckyguy.sendMessage(ChatColor.GREEN + "You just received " + ChatColor.GOLD + Integer.parseInt(args[2]) + " " + CurrencyProperties.Currency);
            } else {
                ply.sendMessage(ChatColor.RED + "That player doesn't exist or is not only");
            }
        }
        return true;
    }
}
